package tv.dyndns.kishibe.client.game.input;

import java.util.Set;

import tv.dyndns.kishibe.client.game.AnswerView;
import tv.dyndns.kishibe.client.game.input.InputWidgetTyping.LetterType;
import tv.dyndns.kishibe.client.game.panel.QuestionPanel;
import tv.dyndns.kishibe.client.packet.PacketProblem;
import tv.dyndns.kishibe.client.util.StringUtils;

import com.google.common.collect.Sets;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;

public class InputWidgetTypingQwerty extends InputWidget implements ClickHandler {
	private static final String STYLE_NAME_LETTER = "gwt-Button-typing";
	private static final String STYLE_NAME_CONTROL = "gwt-Button-typingControl";
	private static final String[] LETTERS = { "１２３４５６７８９０-", "ＱＷＥＲＴＹＵＩＯＰ", "ＡＳＤＦＧＨＪＫＬ", "ＺＸＣＶＢＮＭ" };
	private final LetterType letterType;
	private final Set<Button> buttons = Sets.newHashSet();
	private final Button buttonDelete = new Button("消す", this);
	private final Button buttonOk = new Button("OK", this);

	public InputWidgetTypingQwerty(PacketProblem problem, AnswerView answerView,
			QuestionPanel questionPanel, LetterType letterType) {
		super(problem, answerView, questionPanel);
		this.letterType = letterType;

		setHorizontalAlignment(ALIGN_LEFT);
		for (int row = 0; row < LETTERS.length; ++row) {
			HorizontalPanel panel = new HorizontalPanel();

			HTML space = new HTML();
			space.setPixelSize(row * 20, 1);
			panel.add(space);

			String letters = LETTERS[row];
			for (int column = 0; column < letters.length(); ++column) {
				Button button = new Button(letters.substring(column, column + 1), this);
				button.setStyleName(STYLE_NAME_LETTER);
				panel.add(button);
				buttons.add(button);
			}

			if (row + 1 == LETTERS.length) {
				panel.add(buttonDelete);
				panel.add(buttonOk);
				buttonDelete.setStyleName(STYLE_NAME_CONTROL);
				buttonOk.setStyleName(STYLE_NAME_CONTROL);
			}

			add(panel);
		}
	}

	@Override
	public void enable(boolean b) {
		for (Button button : buttons) {
			button.setEnabled(b);
		}
		buttonDelete.setEnabled(b);
		buttonOk.setEnabled(b);
	}

	@Override
	public void onClick(ClickEvent event) {
		Button source = (Button) event.getSource();
		if (buttons.contains(source)) {
			// 文字ボタン
			String answer = answerView.get();
			answer += source.getText();
			switch (letterType) {
			case Alphabet:
				break;
			case Hiragana:
				answer = StringUtils.convertLastAlphabetToKana(answer,
						StringUtils.LetterType.Hiragana);
				break;
			case Katakana:
				answer = StringUtils.convertLastAlphabetToKana(answer,
						StringUtils.LetterType.Katakana);
				break;
			}
			answerView.set(answer, true);

		} else if (source == buttonDelete) {
			// 消すボタン
			String answer = answerView.get();
			answer = StringUtils.removeLast(answer);
			answerView.set(answer, true);

		} else if (source == buttonOk) {
			// OKボタン
			enable(false);
			String answer = answerView.get();
			if (letterType != InputWidgetTyping.LetterType.Alphabet) {
				answer = StringUtils.convertLastAlphabetToKanaBeforeSendAnswer(answer,
						letterType == LetterType.Hiragana ? StringUtils.LetterType.Hiragana
								: StringUtils.LetterType.Katakana);
			}
			answerView.set(answer, true);
			sendAnswer(answer);

		} else {
			throw new AssertionError();
		}
	}
}
